import 'package:flutter/material.dart';

import 'package:get/get.dart';
import '../../../services/screebAdapter.dart';

import '../controllers/cart_controller.dart';
import 'cartItem_view.dart';

class CartView extends GetView {
  final CartController controller = Get.put(CartController());
  CartView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        title: const Text('购物车'),
        centerTitle: true,
        actions: [
          TextButton(onPressed: () {}, child: const Text('编辑')),
        ],
      ),
      body: GetBuilder<CartController>(
        initState: (state) {
          // 获取购物车数据
          controller.getCartListData();
        },
        init: controller,
        builder: (controller) {
          return
              // controller.cartList.isNotEmpty?
              Stack(
            children: [
              // 购物车列表
              Container(
                padding: EdgeInsets.only(bottom: ScreenAdapter.height(190)),
                child: ListView(
                  children: controller.cartList.map((value) {
                    return CartItemView(value);
                  }).toList(),
                ),
              ),
              // 浮动结算
              Positioned(
                  left: 0,
                  right: 0,
                  bottom: 0,
                  child: Container(
                    padding: EdgeInsets.only(
                        left: ScreenAdapter.width(20),
                        right: ScreenAdapter.width(20)),
                    width: ScreenAdapter.width(1080),
                    height: ScreenAdapter.height(190),
                    decoration: const BoxDecoration(
                        color: Colors.white,
                        border: Border(
                            top: BorderSide(
                                color: Color.fromARGB(31, 75, 74, 74),
                                width: 1))),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        // 左侧
                        Row(
                          children: [
                            Checkbox(
                                activeColor: Colors.red,
                                value: controller.checkedAllBox.value,
                                onChanged: (value) {
                                  controller.checkedAll(value);
                                }),
                            const Text('全选')
                          ],
                        ),
                        // 右侧
                        Row(
                          children: [
                            const Text('合计'),
                            Container(
                              padding: EdgeInsets.only(
                                  right: ScreenAdapter.width(20)),
                              child: Text(
                                '￥98.9',
                                style: TextStyle(
                                    color: Colors.red,
                                    fontSize: ScreenAdapter.fontSize(48)),
                              ),
                            ),
                            ElevatedButton(
                                style: const ButtonStyle(
                                    backgroundColor:
                                        MaterialStatePropertyAll(Colors.red),
                                    foregroundColor:
                                        MaterialStatePropertyAll(Colors.white)),
                                onPressed: () {
                                  // 调用结算方法
                                  // controller.checkout();
                                  Get.toNamed('/checkout');
                                },
                                child: const Text('结算'))
                          ],
                        )
                      ],
                    ),
                  ))
            ],
          );
          // : const Center(
          //     child: Text('购物车空空如也'),
          //   );
        },
      ),
    );
  }
}
